package com.nju.edu.erp.service.hr;

import com.nju.edu.erp.dao.SaleSheetDao;
import com.nju.edu.erp.dao.StuffDao;
import com.nju.edu.erp.model.po.StuffPO;

import java.math.BigDecimal;

public class GetSalaryImpl implements GetSalary{

    private final StuffDao stuffDao;
    private final SaleSheetDao saleSheetDao;
    public GetSalaryImpl(StuffDao stuffDao,SaleSheetDao saleSheetDao){
        this.stuffDao=stuffDao;
        this.saleSheetDao=saleSheetDao;
    }
    @Override
    public BigDecimal getSalaryByName(String name) {

        BigDecimal salaryWithTax=step1(name);
        BigDecimal tax=step2(salaryWithTax);
        //计算去税的工资
        BigDecimal salaryWithoutTax=salaryWithTax.subtract(tax);
        return salaryWithoutTax;
    }

    @Override
    public BigDecimal getTaxByName(String name) {
        BigDecimal salaryWithTax=step1(name);
        BigDecimal tax=step2(salaryWithTax);
        return tax;
    }
    //得到带税薪资
    private BigDecimal step1(String name){
        StuffPO stuffPO=stuffDao.getStuffByName(name);
        Integer salaryCalculateWay=stuffPO.getSalaryCalculateWay();
        GetSalaryWithTax getSalaryWithTax = null;
        switch (salaryCalculateWay){
            case 1:
                getSalaryWithTax=new MonthPayOnly(stuffDao);
                break;
            case 2:
                getSalaryWithTax=new MonthPayWithCommision(stuffDao,saleSheetDao);
                break;
            case 3:
                getSalaryWithTax=new YearPay(stuffDao);
        }
        BigDecimal salaryWithTax=getSalaryWithTax.getSalaryWithTax(name);
        return salaryWithTax;
    }
    //计算税额
    private BigDecimal step2(BigDecimal salaryWithTax){
        Double[] salaryLevel={5000.00, 8000.00,12000.00};
        Double[] taxRateLevel={0.00,0.01,0.02,0.03};
        BigDecimal taxRate=BigDecimal.valueOf(taxRateLevel[salaryLevel.length]);
        for(int i=0;i<salaryLevel.length;i++){
            boolean ifLower=salaryWithTax.compareTo(BigDecimal.valueOf(salaryLevel[i]))==-1;
            if(ifLower){
                taxRate= BigDecimal.valueOf(taxRateLevel[i]);
                break;
            }
        }
        return salaryWithTax.multiply(taxRate);
    }
}
